package com.lvbaoins.Harmony.quartz.config;

import com.lvbaoins.Harmony.model.exception.TaskException;
import com.lvbaoins.Harmony.quartz.domain.SysJob;
import com.lvbaoins.Harmony.quartz.service.SysJobService;
import com.lvbaoins.Harmony.quartz.util.ScheduleUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author : gjb
 * @description:
 * @date : 2020-10-19 14:01
 * @version: : v1.0
 */
@Configuration
@Slf4j
public class InitJob {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private SysJobService sysJobService;


    @PostConstruct
    private void initJob() throws SchedulerException, TaskException {
        scheduler.clear();
        List<SysJob> jobList = sysJobService.list();
        for (SysJob job : jobList)
        {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
        log.info("定时任务加载初始化完毕" );
    }

}
